/*
 * <one line to give the program's name and a brief idea of what it does.>
 * Copyright (C) 2019
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "dbmemory.hpp"
#include <cstring>

DBMemory::DBMemory(const std::size_t length) : DataBase() {
	m_Length = length;
}

DBMemory::~DBMemory() {
	erase();
}

bool DBMemory::empty() {
	std::lock_guard<std::mutex> lock(m_Mutex);
	return m_Data.size() == 0;
}

void DBMemory::erase() {
	std::lock_guard<std::mutex> lock(m_Mutex);

	while(!m_Data.empty()) {
		delete [] m_Data.front();
		m_Data.pop();
	}
}

bool DBMemory::get(void * data, const std::size_t & size) {
	bool result = false;

	if(size <= m_Length) {
		std::lock_guard<std::mutex> lock(m_Mutex);

		if(m_Data.size() > 0) {
			char * d = m_Data.front();
			std::memcpy(data, d, size);
			m_Data.pop();
			delete [] d;
			result = true;
		}
	}

	return result;
}

void DBMemory::put(void * data, const std::size_t & size) {
	if(size <= m_Length) {
		char * d = new char[m_Length];
		std::memcpy(d, data, size);
		std::lock_guard<std::mutex> lock(m_Mutex);
		m_Data.push(d);
	}
}

std::size_t DBMemory::size() {
	std::lock_guard<std::mutex> lock(m_Mutex);
	return m_Data.size();
}

std::size_t DBMemory::addBuilds(std::vector<PathData>* data, PathData* build, DataBase* queue)
{
	return 0;
}

bool DBMemory::updateBuild(PathData* data)
{
	return false;
}




